
// <copyright file="SafeHashSet.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The safe hash set.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Concurrent
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    using SubhadraSolutions.Sharp.Utils.Threading;

    /// <summary>
    /// The safe hash set.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public sealed class SafeHashSet<T> : AbstractReaderWriterAtomicOperationSupported, ICollection<T>
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly HashSet<T> adaptedObject = new HashSet<T>();

        #endregion Fields

        #region Public Properties

        /// <summary>
        /// Gets the count.
        /// </summary>
        public int Count
        {
            get
            {
                return this.adaptedObject.Count;
            }
        }

        /// <summary>
        /// Gets a value indicating whether is read only.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Add(T item)
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                return this.adaptedObject.Add(item);
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        /// <summary>
        /// The clear.
        /// </summary>
        public void Clear()
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                this.adaptedObject.Clear();
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        /// <summary>
        /// The contains.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Contains(T item)
        {
            this.LockSlim.EnterReadLock();
            try
            {
                return this.adaptedObject.Contains(item);
            }
            finally
            {
                this.LockSlim.ExitReadLock();
            }
        }

        /// <summary>
        /// The copy to.
        /// </summary>
        /// <param name="array">
        /// The array.
        /// </param>
        /// <param name="arrayIndex">
        /// The array index.
        /// </param>
        public void CopyTo(T[] array, int arrayIndex)
        {
            try
            {
                this.LockSlim.EnterReadLock();
                this.adaptedObject.CopyTo(array, arrayIndex);
            }
            finally
            {
                this.LockSlim.ExitReadLock();
            }
        }

        /// <summary>
        /// The get all.
        /// </summary>
        /// <returns>
        /// The <see cref="List"/>.
        /// </returns>
        public List<T> GetAll()
        {
            this.LockSlim.EnterReadLock();
            try
            {
                return this.adaptedObject.ToList();
            }
            finally
            {
                this.LockSlim.ExitReadLock();
            }
        }

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            return new SafeEnumeratorDecorator<T>(this.adaptedObject.GetEnumerator(), this.LockSlim);
        }

        /// <summary>
        /// The remove.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Remove(T item)
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                return this.adaptedObject.Remove(item);
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }
        
        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        void ICollection<T>.Add(T item)
        {
            this.Add(item);
        }

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion Explicit Interface Methods

        #region Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            this.LockSlim.Dispose();
        }

        #endregion Methods
    }
}